﻿using System;
using System.Data;
using Koonsoft.HYCRM.DAL;
using log4net;

namespace Koonsoft.HYCRM.BLL
{
    public class YearAuditBLL
    {
        private ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private YearAuditDAL _dal = null;

        protected YearAuditDAL DAL
        {
            get
            {
                if (_dal == null)
                {
                    _dal = new YearAuditDAL();
                }

                return _dal;
            }
        }

        /// <summary>
        /// 导出待年检记录
        /// </summary>
        /// <param name="month">月份</param>
        /// <param name="branchID">分行编号</param>
        /// <returns></returns>
        public DataTable GetAnnualCheckMissions(int month, int branchID, int year)
        {
            return DAL.GetAnnualCheckMissions(month, branchID, year);
        }

        /// <summary>
        /// 按工号获得员工待办的年检任务
        /// </summary>
        /// <param name="employeeID"></param>
        /// <returns></returns>
        public DataTable GetYearAuditTasksByEmpolyeeID(int employeeID)
        {
            DataTable mytasks = new DataTable();

            try
            {
                // 只显示从今日起90天以内的任务
                DataTable dt = DAL.GetYearAuditTasksByEmpolyeeID(employeeID);

                mytasks = dt.Copy();
                mytasks.Rows.Clear();

                foreach (DataRow dr in dt.Rows)
                {
                    DateTime auditDate = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + dr["auditmonth"] + "-" + dr["auditday"]);

                    if (auditDate < DateTime.Now.AddDays(90) && auditDate >= DateTime.Now)
                    {
                        mytasks.ImportRow(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("按工号获得员工待办的年检任务异常", ex);
            }

            return mytasks;
        }
    }
}
